-
Notifications
You must be signed in to change notification settings - Fork 127
fix(data): Prevent semaphore deadlock within PersistentMutationOutbox #3115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
# Conflicts: # aws-datastore/src/main/java/com/amplifyframework/datastore/syncengine/PersistentMutationOutbox.kt
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concept looks good. Instead of repeating the logic could we make an extension function like this?
fun Completable.acquireSemaphore(semaphore: Semaphore): Completable {
val semaphoreReleased = AtomicBoolean(false)
return this.doOnSubscribe { semaphore.acquire() }
.doOnTerminate {
if (semaphoreReleased.compareAndSet(false, true)) {
semaphore.release()
}
}
.doFinally {
if (semaphoreReleased.compareAndSet(false, true)) {
semaphore.release()
}
}
}|
Good suggestion. Done |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #3115 +/- ##
=======================================
Coverage 54.22% 54.23%
=======================================
Files 1038 1038
Lines 31989 32002 +13
Branches 4702 4704 +2
=======================================
+ Hits 17346 17356 +10
- Misses 12795 12799 +4
+ Partials 1848 1847 -1 🚀 New features to boost your workflow:
|
Issue #, if available:
Description of changes:
There's a scenario here that can happen with constant start/stops where neither
doOnTerminateanddoOnErrorget called to release the semaphore. If a completable is unsubscribed, doOnTerminate does not get called.doFinallywill always get called, however, it runs on a different thread. This should be safe, and seemed to work ok if we replaceddoOnTerminateentirely, but given the threading model, its best to release indoOnTerminate. ThedoFinallyis a new safety check that will only release if the semaphore was not already released indoOnTerminate.How did you test these changes?
(Please add a line here how the changes were tested)
Documentation update required?
General Checklist
fix(storage): message,feat(auth): message,chore(all): message)By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.